﻿using Iworld.Administrator.Model;
using Iworld.basis.Service;
using Iworld.Basis.Manager;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;

namespace Iworld.Administrator.Service.Import
{
    /// <summary>
    /// 创建管理员组的数据集
    /// </summary>
   [DataContract]
    public class CreateAdministratorGroupImport : CheckAdministratorImportBase, IPackageForCreateModel<IModelToDbContextOfAdministrator,
         Iworld.Administrator.Model.AdministratorGroup>
    {

        [DataMember]
        public string Name { set; get; }
        /// <summary>
        /// 能否编辑牌局
        /// </summary>
        [DataMember]
        public bool CanEditGameRound { set; get; }

        /// <summary>
        /// 能否查看牌局信息
        /// </summary>
        [DataMember]
        public bool CanViewGameRound { set; get; }

        /// <summary>
        /// 是否能查看牌轮信息
        /// </summary>
        [DataMember]
        public bool CanViewGameChapter { set; get; }

        /// <summary>
        /// 能否编辑前台用户
        /// </summary>
        [DataMember]
        public bool CanEditUser { set; get; }

        /// <summary>
        /// 能否编辑游戏设置
        /// </summary>
        [DataMember]
        public bool CanEditGameSetting { set; get; }

        /// <summary>
        /// 能否生成充值卡
        /// </summary>
        [DataMember]
        public bool CanCreateRechargeCard { set; get; }

        /// <summary>
        /// 能否编辑管理员
        /// </summary>
        [DataMember]
        public bool CanEditAdministrator { set; get; }

        /// <summary>
        /// 能否编辑管理员用户组
        /// </summary>
        [DataMember]
        public bool CanEditAdministratorGroup { set; get; }

        /// <summary>
        /// 是否处理提现
        /// </summary>
        [DataMember]
        public bool CanReviewWithdrawals { set; get; }

        public override void CheckAllowExecuteOrNot(Main.Impl.ModelToDbContext db)
        {
            base.CheckAllowExecuteOrNot(db);
            if(!Self.Group.CanEditAdministratorGroup)
            {
                throw new Exception("你无权修改或添加管理员组");
            }

        }

        public void CheckData(IModelToDbContextOfAdministrator db)
        {
            if(string.IsNullOrWhiteSpace(this.Name))
            {
                throw new Exception("请输入正确的名字");
            }
            bool hadUsedUsername = db.AdministratorGroups.Any(x => x.Name == this.Name);
            if (hadUsedUsername)
            {
                string error = string.Format("组名称 {0} 已经被使用", this.Name);
                throw new Exception(error);
            }
        }

        public AdministratorGroup GetModel(IModelToDbContextOfAdministrator db)
        {
            return new AdministratorGroup() { 
                Name = this.Name, 
                CanCreateRechargeCard=this.CanCreateRechargeCard,
                CanEditAdministrator = this.CanEditAdministrator,
                CanEditAdministratorGroup = this.CanEditAdministratorGroup,
                CanEditGameRound = this.CanEditGameRound,
                CanEditGameSetting = this.CanEditGameSetting,
                CanEditUser = this.CanEditUser,
                CanViewGameChapter = this.CanViewGameChapter,
                CanViewGameRound = this.CanViewGameRound, 
                CanReviewWithdrawals = this.CanReviewWithdrawals,
                AdministratorId=this.Self.Id
            };
        }
    }
}
